package com.xyf.sys.service.impl;

import cn.hutool.core.util.IdUtil;
import com.xyf.sys.domain.OrderProductCode;
import com.xyf.sys.domain.User;
import com.xyf.sys.mapper.OrderProductCodeMapper;
import com.xyf.sys.service.IOrderProductCodeService;
import com.xyf.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Author R-H
 * @Date 2024/9/191106
 * @Description
 */
@Service
public class OrderProductCodeServiceImpl implements IOrderProductCodeService {

    private static final AtomicLong counter = new AtomicLong(100000L);

    @Autowired
    private OrderProductCodeMapper mapper;
    @Autowired
    private IUserService userService;

    @Override
    public List<String> batchGenerate(Integer size, String regimentId, String userId) {
        User regiment = userService.getUser(regimentId);
        Integer batchNo = getNextBatchNo(regimentId);
        return IntStream.range(0, size).mapToObj(i -> {
            OrderProductCode orderProductCode = new OrderProductCode();
            orderProductCode.setId(IdUtil.getSnowflakeNextIdStr());
            orderProductCode.setCode(generateUnique1Code());
            orderProductCode.setBatchNo(batchNo);
            orderProductCode.setUserId(regiment.getId());
            orderProductCode.setLastUpdateTime(new Date());
            orderProductCode.setCreateTime(new Date());
            orderProductCode.setCreateBy(userId);
            orderProductCode.setStatus(0);
            orderProductCode.setDelFlag("0");
            mapper.insert(orderProductCode);
            return orderProductCode.getCode();
        }).collect(Collectors.toList());
    }

    @Override
    public Integer getNextBatchNo(String userId) {
        return mapper.selectMaxBatchNo(userId) + 1;
    }

    @Override
    public OrderProductCode get(String code) {
        return mapper.selectByCode(code);
    }

    @Override
    public int update(OrderProductCode code) {
        return mapper.updateByPrimaryKeySelective(code);
    }

    @Override
    public void save(OrderProductCode orderProductCode) {
        orderProductCode.setLastUpdateTime(new Date());
        orderProductCode.setCreateTime(new Date());
        orderProductCode.setDelFlag("0");
        mapper.insert(orderProductCode);
    }

    // 生成并返回下一个唯一的12位数字
    public static synchronized String generateUnique12DigitNumber() {
        // 确保计数器的值不超过12位数字的最大值
        long nextValue = counter.incrementAndGet();
        if (nextValue > 999999L) {
            throw new IllegalStateException("无法生成更多的12位唯一数字");
        }
        // 格式化为12位字符串，不足前面补0
        return String.format("%06d", nextValue);
    }

    public String generateUnique1Code() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        // 生成 12 位随机纯数字
        for (int i = 0; i < 12; i++) {
            int digit = random.nextInt(10); // 生成 0 到 9 的随机数字
            sb.append(digit);
        }
        String randomNumber = sb.toString();
        return this.mapper.selectByCode(randomNumber) == null ? randomNumber : generateUnique1Code();
    }
}
